/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */


package com.iwindplus.log.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.iwindplus.base.domain.constant.CommonConstant;
import com.iwindplus.base.domain.enums.BizCodeEnum;
import com.iwindplus.base.domain.exception.BizException;
import com.iwindplus.base.mongo.service.impl.MongoBaseServiceImpl;
import com.iwindplus.log.domain.dto.BinlogAlertDTO;
import com.iwindplus.log.domain.dto.BinlogAlertSearchDTO;
import com.iwindplus.log.domain.vo.BinlogAlertPageVO;
import com.iwindplus.log.domain.vo.BinlogAlertVO;
import com.iwindplus.log.server.dal.model.BinlogAlertDO;
import com.iwindplus.log.server.service.BinlogAlertService;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * binlog告警业务层接口实现类.
 *
 * @author zengdegui
 * @since 2018/9/1
 */

@Slf4j
@Service
@CacheConfig(cacheNames = {"binlogAlert"})
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class BinlogAlertServiceImpl extends MongoBaseServiceImpl<BinlogAlertDO>
    implements BinlogAlertService {

    @CacheEvict(allEntries = true)
    @Override
    public boolean save(BinlogAlertDTO entity) {
        final BinlogAlertDO model = BeanUtil.copyProperties(entity, BinlogAlertDO.class);
        super.save(model);
        entity.setId(model.getId());
        return Boolean.TRUE;
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean removeByIds(List<String> ids) {
        final Criteria criteria = Criteria.where(CommonConstant.DbConstant.ID).in(ids);
        final Query query = Query.query(criteria);
        List<BinlogAlertDO> data = super.list(query);
        if (CollUtil.isEmpty(data)) {
            throw new BizException(BizCodeEnum.DATA_NOT_EXIST);
        }
        super.removeByIds(ids, false);
        return Boolean.TRUE;
    }

    @Override
    public IPage<BinlogAlertPageVO> page(BinlogAlertSearchDTO entity) {
        final PageDTO<BinlogAlertDO> page = new PageDTO<>(entity.getCurrent(), entity.getSize());
        Criteria criteria = new Criteria();
        if (Objects.nonNull(entity.getDataId())) {
            criteria.and("dataId").is(entity.getDataId());
        }
        if (CharSequenceUtil.isNotBlank(entity.getDb())) {
            criteria.and("db").is(entity.getDb().trim());
        }
        if (CharSequenceUtil.isNotBlank(entity.getTable())) {
            criteria.and("table").is(entity.getTable().trim());
        }
        Query query = Query.query(criteria);
        List<OrderItem> orders = page.getOrders();
        if (CollUtil.isEmpty(orders)) {
            orders = new ArrayList<>(10);
            OrderItem orderItem = OrderItem.desc("modifiedTime");
            orders.add(orderItem);
            page.setOrders(orders);
        }
        final IPage<BinlogAlertDO> modelPage = super.page(page, query);
        return modelPage.convert(model -> BeanUtil.copyProperties(model, BinlogAlertPageVO.class));
    }

    @Cacheable(key = "#root.methodName + '_' + #p0", condition = "#p0 != null", unless = "#result == null")
    @Override
    public BinlogAlertVO getDetail(String id) {
        BinlogAlertDO data = super.getById(id);
        if (Objects.isNull(data)) {
            throw new BizException(BizCodeEnum.DATA_NOT_EXIST);
        }
        return BeanUtil.copyProperties(data, BinlogAlertVO.class);
    }

}
